package org.coc.iot.tag;

import freemarker.template.TemplateModelException;
import org.coc.iot.common.utils.Utils;
import org.coc.iot.ehcache.CacheConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Created by Bean on 2017/5/19.
 */
@Configuration
public class TagConfig {

    @Value("${template.taglib}")
    private String tagVariable;

    @Autowired
    protected freemarker.template.Configuration configuration;

    @Autowired
    private CacheConfiguration cacheConfiguration;

    @PostConstruct
    public void setSharedVariable() throws Exception {
        if(!Utils.strIsNull(tagVariable)) {
            String[] tagArr = tagVariable.split(",");
            for(String tag : tagArr) {
                tag = Utils.trim(tag);
                String tagName = tag.substring(tag.lastIndexOf(".") + 1);
                configuration.setSharedVariable(tagName, Class.forName(tag).newInstance());
            }
        }
    }
}
